package com.sky.controller;

import cn.hutool.core.bean.BeanUtil;
import com.sky.api.coupon.CouponFeignService;
import com.sky.domain.UmsMember;
import com.sky.dto.member.UmsMemberVO;
import com.sky.entity.GiteeEntity;
import com.sky.exception.BizCodeEnum;
import com.sky.service.UmsMemberService;
import com.sky.utils.PageUtils;
import com.sky.utils.R;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/umsMember")
public class UmsMemberController {

    @Resource
    private UmsMemberService umsMemberService;

    @RequestMapping("/list")
    public Object list() {
        return null;
    }

    @GetMapping("/page")
    public Object page(UmsMemberVO vo) {
        PageUtils page = umsMemberService.queryPage(vo);
        return R.ok(page);
    }

    @PostMapping("/addUmsMember")
    public R<Boolean> addUmsMember(@RequestBody UmsMemberVO vo) {
        try {
            boolean flag = umsMemberService.addUmsMember(vo);
            return R.ok(flag);
        } catch (Exception e) {
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
        }
    }

    @PostMapping("/login")
    public R<UmsMemberVO> login(@RequestBody UmsMemberVO vo) {
        UmsMember umsMember = umsMemberService.login(vo);
        if (umsMember != null){
            UmsMemberVO umsMemberVO = BeanUtil.copyProperties(umsMember, UmsMemberVO.class);
            return R.ok(umsMemberVO);
        } else {
            return R.error(BizCodeEnum.USERNAME_PASSWORD_EXCEPTION.getCode(), BizCodeEnum.USERNAME_PASSWORD_EXCEPTION.getMsg());
        }
    }

    @PostMapping("/giteeLoginByToken")
    public R<UmsMemberVO> giteeLoginByToken(@RequestBody GiteeEntity giteeEntity){
        UmsMember umsMember = umsMemberService.giteeLoginByToken(giteeEntity);
        if (umsMember != null){
            UmsMemberVO vo = BeanUtil.copyProperties(umsMember, UmsMemberVO.class);
            return R.ok(vo);
        } else {
            return R.error(BizCodeEnum.USERNAME_PASSWORD_EXCEPTION.getCode(), BizCodeEnum.USERNAME_PASSWORD_EXCEPTION.getMsg());
        }
    }

    @GetMapping("/getMemberById")
    public R<UmsMemberVO> getMemberById(Long memberId){
        UmsMember umsMember = umsMemberService.getById(memberId);
        UmsMemberVO vo = BeanUtil.copyProperties(umsMember, UmsMemberVO.class);
        return R.ok(vo);
    }
}
